package com.ktgj.flight.service.impl;

import com.ktgj.flight.domain.Airport;
import com.ktgj.flight.domain.vo.AirportsVo;
import com.ktgj.flight.domain.vo.ClassificationVo;
import com.ktgj.flight.domain.vo.DomesticCitiesVo;
import com.ktgj.flight.mapper.AirportMapper;
import com.ktgj.flight.service.IAirportService;
import com.ktgj.flight.vo.AirportVO;
import com.ktgj.hotel.api.RemoteHotelService;
import net.sourceforge.pinyin4j.PinyinHelper;
import net.sourceforge.pinyin4j.format.HanyuPinyinCaseType;
import net.sourceforge.pinyin4j.format.HanyuPinyinOutputFormat;
import net.sourceforge.pinyin4j.format.HanyuPinyinToneType;
import net.sourceforge.pinyin4j.format.exception.BadHanyuPinyinOutputFormatCombination;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * 机场Service业务层处理
 * 
 * @author ruoyi
 * @date 2021-08-30
 */
@Service
public class AirportServiceImpl implements IAirportService
{
    @Autowired
    private AirportMapper airportMapper;
    @Autowired
    private RemoteHotelService remoteHotelService;

    /**
     * 获取机场名称
     * */
    @Override
    public String airportNameById(Long id) {
        Airport airport = airportMapper.airportNameById(id);
        return airport.getAirportName();
    }

    /**
     * 查询机场
     * 
     * @param id 机场ID
     * @return 机场
     */
    @Override
    public Airport selectAirportById(Long id)
    {
        return airportMapper.selectAirportById(id);
    }

    /**
     * 查询机场列表
     * 
     * @param airport 机场
     * @return 机场
     */
    @Override
    public List<Airport> selectAirportList(Airport airport)
    {
        return airportMapper.selectAirportList(airport);
    }

    /**
     * 新增机场
     * 
     * @param airport 机场
     * @return 结果
     */
    @Override
    public int insertAirport(Airport airport)
    {
        Map<String, Object> map = new HashMap<>();
        map.put("name", airport.getAirportCityName());
        Long cityId = remoteHotelService.selectCityId(map);
        airport.setAirportCityId(cityId.toString());
        return airportMapper.insertAirport(airport);
    }

    /**
     * 修改机场
     * 
     * @param airport 机场
     * @return 结果
     */
    @Override
    public int updateAirport(Airport airport)
    {
        return airportMapper.updateAirport(airport);
    }

    /**
     * 批量删除机场
     * 
     * @param ids 需要删除的机场ID
     * @return 结果
     */
    @Override
    public int deleteAirportByIds(Long[] ids)
    {
        return airportMapper.deleteAirportByIds(ids);
    }

    /**
     * 删除机场信息
     * 
     * @param id 机场ID
     * @return 结果
     */
    @Override
    public int deleteAirportById(Long id)
    {
        return airportMapper.deleteAirportById(id);
    }

    /**
     *  查询热门机场
     * @param
     * @return 机场列表
     * */
    @Override
    public List<AirportVO> selectAirportByRemenAirport() {
        return airportMapper.selectAirportByRemenAirport();
    }

    /**
     *  机场表搜索功能
     * @param airportName 机场名称
     * @return 机场列表
     * */
    @Override
    public List<AirportVO> selectAirportByAirportName(String airportName) {
        return airportMapper.selectAirportByAirportName("%"+airportName+"%");
    }

    /**
     * 机场表搜索功能
     * @return
     */
    @Override
    public List<AirportVO> selectAirportByAirport() {
        return airportMapper.selectAirportByAirport();
    }

    /**
     * 查询机场列表 Airport表
     * @param
     * @return 根据首字母排序的机场列表
     * */
    @Override
    public List<AirportVO> selectAirport() {
        List<Airport> list = airportMapper.selectAirport();
        List<AirportVO> airportVOList = new ArrayList<>();
        //遍历集合，将需要的数据去除，填充到airportVOList中
        for (Airport item : list) {
            AirportVO airportVO = new AirportVO();
            airportVO.setAirportId(item.getAirportId());
            airportVO.setAirportName(item.getAirportName());
            String letter=item.getAirportNameJianpin().substring(0,1);
            String uppercaseLetter = letter.toUpperCase();
            airportVO.setLetter(uppercaseLetter);
            airportVOList.add(airportVO);
        }
        return airportVOList;
    }

    /**
     * 根据城市名称查询对应机场经纬度
     * @param airportCityName 城市名称
     * @return 机场列表
     * */
    @Override
    public List<Airport> selectAirportByAirportCityName(String airportCityName) {
        return airportMapper.selectAirportByAirportCityName(airportCityName);
    }

    @Override
    public Airport getAirPortNameByRirPortCode(String airPortCode) {
        return airportMapper.getAirPortNameByRirPortCode(airPortCode);
    }

    @Override
    public String queryAirPortName(String airportId) {
        return airportMapper.queryAirPortName(airportId);
    }

    /**
     * 根据机场名称获取机场三字码
     * @param airportName
     * @return
     */
    @Override
    public Airport getAirportInfoByAirportName(String airportName) {
        return airportMapper.getAirportInfoByAirportName(airportName);
    }

    @Override
    public AirportsVo selectAirportCity(String airportCityName) {
        String[] letterList = {"热门", "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M",
                "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z"};
        AirportsVo airportsVo = new AirportsVo();
        List<ClassificationVo> classification = new ArrayList<>();
        List<Airport> airports = airportMapper.selectAirportLike("%" + airportCityName + "%");
        if (airports.size() > 0){
            for (int i = 0 ; i < letterList.length ; ++i){
                ClassificationVo classificationVo = new ClassificationVo();
                classificationVo.setLetter(letterList[i]);
                List<DomesticCitiesVo> domesticCities = new ArrayList<>();
                for (Airport airport : airports) {
                    String pinYinHeadChar = getPinYinHeadChar(airport.getAirportCityName()).substring(0, 1);//获取城市首字母
                    if ((letterList[i].equals(pinYinHeadChar)) ||
                            (letterList[i].equals("热门") && airport.getPopular().equals("true"))){
                        DomesticCitiesVo citiesVo = new DomesticCitiesVo();
                        citiesVo.setId(airport.getId());
                        citiesVo.setAirportCityName(airport.getAirportCityName());
                        citiesVo.setAirportId(airport.getAirportId());
                        citiesVo.setAirportName(airport.getAirportName());
                        citiesVo.setAirportCityId(airport.getAirportCityId());
                        domesticCities.add(citiesVo);
                    }
                }
                classificationVo.setDomesticCities(domesticCities);
                if (classificationVo.getDomesticCities().size() > 0){//去除空集合
                    classification.add(classificationVo);
                }
            }
            airportsVo.setClassification(classification);
        }
        return airportsVo;
    }

    @Override
    public List<Airport> selectAirportLike(String airportCityName) {
        return airportMapper.selectAirportLike(airportCityName);
    }

    /**
     * 获取城市名称
     * */
    @Override
    public List<Airport> selectAirportName(String airportCityId) {
        airportMapper.selectAirportLike(airportCityId);
        return null;
    }

    @Override
    public String queryAirportCity(String airportId) {
        return airportMapper.queryAirportCity(airportId);
    }

    /**
     * 三字码获取机场名称
     * */
    @Override
    public Airport airportName(String airportId) {
        return airportMapper.airportName(airportId);
    }

    /**
     * 机票城市列表
     * */
    @Override
    public List<Map<String, String>> cityList() {
        List<Map<String, String>> mapList = new ArrayList<>();
        List<Airport> airportList = airportMapper.queryAirportList();
        Set<String> set = new HashSet<>();
        Set<String> citySet = new HashSet<>();
        airportList.forEach(item -> {
            Map<String, String> map = new HashMap<>();
            Map<String, String> cityMap = new HashMap<>();
            if (!item.getAirportCityName().equals(item.getAirnamesort()))
                map.put("airportCityName", item.getAirportCityName() + item.getAirnamesort());
            else
                map.put("airportCityName", item.getAirportCityName());
            map.put("airportId", item.getAirportId());
            map.put("airportCityJpinyin", item.getAirportCityJpinyin().toLowerCase());
            map.put("airportCityPinyin", item.getAirportCityPinyin().toLowerCase());
            mapList.add(map);
            if (!set.add(item.getAirportCityName())) {
                if (citySet.add(item.getAirportCityName())) {
                    cityMap.put("airportCityName", item.getAirportCityName());
                    cityMap.put("airportId", item.getAirportCityId());
                    cityMap.put("airportCityJpinyin", item.getAirportCityJpinyin().toLowerCase());
                    cityMap.put("airportCityPinyin", item.getAirportCityPinyin().toLowerCase());
                    mapList.add(cityMap);
                }

            }
        });
        return mapList;
    }

    /**
     * 获取中文首字母
     * */
    public static String getPinYinHeadChar(String str) {
        StringBuilder convert = new StringBuilder();
        for (int j = 0; j < str.length(); j++) {
            char word = str.charAt(j);
            String[] pinyinArray = PinyinHelper.toHanyuPinyinStringArray(word);
            if (pinyinArray != null) {
                convert.append(pinyinArray[0].charAt(0));
            } else {
                convert.append(word);
            }
        }
        String toUpperCase = convert.toString().toUpperCase();
        return toUpperCase;
    }

    /**
     * 获取汉字串拼音，英文字符不变
     * @param chinese 汉字串
     * @return 汉语拼音
     */
    public static String getFullSpell(String chinese) {
        StringBuffer pybf = new StringBuffer();
        char[] arr = chinese.toCharArray();
        HanyuPinyinOutputFormat defaultFormat = new HanyuPinyinOutputFormat();
        defaultFormat.setCaseType(HanyuPinyinCaseType.UPPERCASE);
        defaultFormat.setToneType(HanyuPinyinToneType.WITHOUT_TONE);
        for (int i = 0; i < arr.length; i++) {
            if (arr[i] > 128) {
                try {
                    pybf.append(PinyinHelper.toHanyuPinyinStringArray(arr[i], defaultFormat)[0]);
                } catch (BadHanyuPinyinOutputFormatCombination e) {
                    e.printStackTrace();
                }
            } else {
                pybf.append(arr[i]);
            }
        }
        return pybf.toString();
    }

}
